/**
 * 
 */
package com.imooc.security.core.social.gitee.api;

import org.apache.commons.lang.StringUtils;
import org.springframework.social.oauth2.AbstractOAuth2ApiBinding;
import org.springframework.social.oauth2.TokenStrategy;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author zhailiang
 *用来做前5步的
 */
public class GiteeImpl extends AbstractOAuth2ApiBinding implements Gitee {

	//通过token获取用户信息的地址
	private static final String URL_GET_USERINFO = "https://gitee.com/api/v5/user?access_token=%s";
	


	private ObjectMapper objectMapper = new ObjectMapper();
	
	public GiteeImpl(String accessToken) {
		//TokenStrategy.AUTHORIZATION_HEADER 这种是放到请求头上
		//TokenStrategy.ACCESS_TOKEN_PARAMETER 是Access_token做为参数来使用
		//第二个参数为
		super(accessToken, TokenStrategy.ACCESS_TOKEN_PARAMETER);

		
	}
	
	/* (non-Javadoc)
	 * @see com.imooc.security.core.social.qq.api.QQ#getUserInfo()
	 */
	@Override
	public GiteeInfo getUserInfo() {
		

		String result = getRestTemplate().getForObject(URL_GET_USERINFO, String.class);
		
		System.out.println(result);
		
		GiteeInfo userInfo = null;
		try {
			userInfo = objectMapper.readValue(result, GiteeInfo.class);
			return userInfo;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException("获取用户信息失败", e);
		}
	}

}
